﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation
{
    public partial class UserForm : Form
    {
        private DataTable m_UserData = new DataTable();
        public DataTable UserData
        {
            get { return m_UserData; }
            set { m_UserData = value; }
        }

        public UserForm()
        {
            InitializeComponent();
        }

        private void UserForm_Load(object sender, EventArgs e)
        {
            dataGridView.DataSource = m_UserData;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            m_UserData.Rows.Clear();
            foreach (DataGridViewRow row in this.dataGridView.Rows)
            {
                decimal id = Convert.ToDecimal(row.Cells["IdColumn"].Value);
                string userName = row.Cells["UserColumn"].Value.ToString();
                string employeeName = row.Cells["EmployeeNameColumn"].Value.ToString();
                bool isAdmin = Convert.ToBoolean(row.Cells["IsAdminColumn"].Value);
                m_UserData.Rows.Add(id, userName, employeeName, isAdmin);
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4)//当单击复选框
            {
                if (Convert.ToString(dataGridView.Rows[e.RowIndex].Cells["IsAdminColumn"].Value) == "true")
                {
                    if ((dataGridView.Rows[e.RowIndex].Cells["UserColumn"].Value).ToString().ToUpper() == "SYSTEM")
                    {
                        MessageBox.Show("系统管理员不能去掉管理员权限!");
                        return;
                    }
                    dataGridView.Rows[e.RowIndex].Cells["IsAdminColumn"].Value = "false";
                }
                else
                {
                    dataGridView.Rows[e.RowIndex].Cells["IsAdminColumn"].Value = "true";
                }
            }
        }
    }
}
